package com.example.shaomengjie.smvp.presenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.shaomengjie.smvp.MyApplication;
import com.example.shaomengjie.smvp.R;
import com.example.shaomengjie.smvp.bean.GuideBean;
import com.example.shaomengjie.smvp.framework.presenter.ActivityPresenter;
import com.example.shaomengjie.smvp.net.callback.OnResultCallBack;
import com.example.shaomengjie.smvp.net.http.HttpMoudle;
import com.example.shaomengjie.smvp.net.subscriber.HttpSubscriber;
import com.example.shaomengjie.smvp.utils.Constant;
import com.example.shaomengjie.smvp.view.SplashDelegate;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class SplashActivity extends ActivityPresenter<SplashDelegate> {
    private static final String JUMP = "0";
    private HttpMoudle httpMoudle;
    private Bundle bundle = new Bundle();
    @Override
    protected Class getDelegateClass() {
        return SplashDelegate.class;
    }
    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        httpMoudle = new HttpMoudle();
        httpMoudle.getGuideData("0", new HttpSubscriber<>(new OnResultCallBack<List<GuideBean>>() {
            @Override
            public void onSuccess(List<GuideBean> guideBean) {
                bundle.putSerializable(Constant.AD, (Serializable) guideBean);
                if (guideBean==null||guideBean.size()==0){return;}
                String img_path = guideBean.get(0).getImg_path();
                if (TextUtils.isEmpty(img_path)){return;}
                viewDelegate.loadSplashADImageView(img_path);
            }
        },this));
        Observable.just(JUMP).delay(300,TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s->{startActivity();});
    }

    private void startActivity() {
        startActivity(new Intent(SplashActivity.this,SplashADActivity.class).putExtras(bundle));
        finish();
    }

    @Override
    protected void onDestroy() {
        httpMoudle = null;
        super.onDestroy();
        viewDelegate = null;
    }
}
